﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.NYBG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;

public partial class EditPM : System.Web.UI.Page
{
    /// <summary>
    /// 能源报告操作对象
    /// </summary>
    private NYBG_DAO nybgDAO = new NYBG_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {

        String message = null;
        //获取分类编码
        String wjm = Request["wjm"];

        NYBG_PM nybgPM = null;
        NYBG_FL nybgFL = null;


        if (null == wjm || "".Equals(wjm))
        {
            message = "错误：文件名为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            nybgPM = this.nybgDAO.GetNYBG_PMByWjm(wjm);
            if (null == nybgPM)
            {
                message = "错误：没有找到对应的文件名";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
            nybgFL = nybgDAO.GetNYBG_FLByFlbm(nybgPM.flbm);
            if (null == nybgFL)
            {
                message = "错误：没有找到对应的分类编码";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalNYBG.LISTPM_PAGE + "?catalog=" + nybgFL.flbm;


        //分类编码
        this.flbmTextBox.Text = nybgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = nybgFL.zwmc;
        //索引编码
        this.sybmTextBox.Text = nybgPM.sybm;
        //文件名
        this.wjmHyperLink.NavigateUrl = GlobalResource.VIEW_PAGE + "?fileName=" + wjm;
        this.wjmHyperLink.Text = wjm;
       
        
        //分类编码
        this.flbmTextBox.Text = nybgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = nybgFL.zwmc;
        //索引编码
        this.sybmTextBox.Text=nybgPM.sybm;

        //年份
        this.nfTextBox.Text = nybgPM.nf.ToString();
       
        //报告名称
        this.bgmcTextBox.Text =nybgPM.bgmc;
        
        //作者
        this.zzTextBox.Text = nybgPM.zz;
        //作者单位
        this.zzdwTextBox.Text = nybgPM.zzdw;
        //主办单位
        this.zbdwTextBox.Text = nybgPM.zbdw;
        //主题词
        this.ztcTextBox.Text =nybgPM.ztc;
        //文摘
        this.wzTextBox.Text = nybgPM.wz;
        //出版日期
        this.cbrqTextBox.Text = nybgPM.cbrq.ToString();
        //报告号
        this.bghTextBox.Text = nybgPM.bgh;

        //分类体系
        this.fltxTextBox.Text = nybgPM.fltx;
        //子分类
        this.zflTextBox.Text = nybgPM.zfl;
        //文献编号
        this.wxbhTextBox.Text = nybgPM.wxbh;
        //更新日期
        this.gxrqTextBox.Text = nybgPM.gxrq.ToString();
        //页数
        this.ysTextBox.Text = nybgPM.ys.ToString();
        //登录人
        this.dlrTextBox.Text = nybgPM.dlr;

        //设置上一篇和下一篇链接
        this.SetPreAndNextPM();
    }

    /// <summary>
    /// 获取用户填写的能源报告告篇名信息
    /// </summary>
    /// <returns></returns>
    private NYBG_PM GetNYBGPM()
    {
        NYBG_PM nybgPM = new NYBG_PM();
        //索引编码
        nybgPM.sybm = this.sybmTextBox.Text;
        //分类编码
        nybgPM.flbm = this.flbmTextBox.Text;
        //文件名
        nybgPM.wjm = this.wjmHyperLink.Text;
        //原文件名
        nybgPM.ywjm = this.ywjmFileUpload.FileName;
        //页数
        nybgPM.ys =Int32.Parse(this.ysTextBox.Text) ;
        //报告名称
        nybgPM.bgmc = this.bgmcTextBox.Text.Trim();
        //年份
        nybgPM.nf = Int32.Parse(this.nfTextBox.Text);
        //出版日期
        nybgPM.cbrq = DateTime.Parse(this.cbrqTextBox.Text);
        //作者
        nybgPM.zz = this.zzTextBox.Text.Trim();
        //作者单位
        nybgPM.zzdw = this.zzdwTextBox.Text.Trim();
        //主办单位
        nybgPM.zbdw= this.zbdwTextBox.Text.Trim();

        //主题词
        nybgPM.ztc = this.ztcTextBox.Text;
        //文摘
        nybgPM.wz = this.wzTextBox.Text;

        //报告号
        nybgPM.bgh = this.bghTextBox.Text;
       
        //分类体系
        nybgPM.fltx = this.fltxTextBox.Text.Trim();
        //子分类
        nybgPM.zfl = this.zflTextBox.Text.Trim();
        //文献编号
        nybgPM.wxbh =this.wxbhTextBox.Text.Trim();
        //页数
        nybgPM.ys = Int32.Parse(this.ysTextBox.Text);
        //全文
        nybgPM.qw = true;
        //更新日期
        nybgPM.gxrq = DateTime.Now;
        //登录人
        nybgPM.dlr = this.dlrTextBox.Text;

        return nybgPM;

    }

    /// <summary>
    /// 获取下一个文件名，包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <returns></returns>
    private String GetWJM(String wjm)
    {
        String result = null;
        if (null == wjm || "".Equals(wjm))
        {
            result = this.sybmTextBox.Text + "-0000.PDF";
        }
        else
        {
            //获取文件名的后四位流水号，并在其基础上加1
            int endIndex = wjm.LastIndexOf(".");

            String phString = wjm.Substring(endIndex - 4, 4);

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >4)
            {
                String message = "错误：篇名数量已经超过了10000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }

            result = this.sybmTextBox.Text + "-" + phString+".PDF";
        }
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateBGMC())
        {
            return;
        }
        if (!this.ValidateWJM())
        {
            return;
        }
        if (!this.ValidateCBRQ())
        {
            return;
        }
        if (!ValidateYWJMFileUpload())
        {
            return;
        }
        
        if (null != this.ywjmFileUpload && null != this.ywjmFileUpload.FileName && !"".Equals(this.ywjmFileUpload.FileName))
        {
            if (!OperationFile.Upload(this.ywjmFileUpload, this.wjmHyperLink.Text))
            {
                Response.Write("<Script>alert('修改能源报告篇名信息失败')</Script>");
                return;
            }
        }

        NYBG_PM nybgPM = this.GetNYBGPM();
        if (this.nybgDAO.UpdateNYBG_PM(nybgPM, ((User)Session["login"]).name))
        {
           
            //this.BindData();
            this.NextPM();
            //Response.Write("<Script>alert('修改能源报告篇名信息成功')</Script>");
            return;
        }
        else
        {
            Response.Write("<Script>alert('修改能源报告篇名信息失败')</Script>");
            return;
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        NYBG_PM nybgPM = this.nybgDAO.GetNYBG_PMByWjm(this.wjmHyperLink.Text);
        if (null == nybgPM)
        {
            this.errorLabel.Text = "错误：要修改的文件名不存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证报告名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBGMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.bgmcTextBox .Text||"".Equals(this.bgmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：报告名称不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            //this.errorLabel.Text = "错误：请选择上传的文件";
            return flag;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysTextBox.Text = pdf.NumberOfPages.ToString();
                }

                pdf.Close();
            }
        }
        return flag;
    }

    /// <summary>
    /// 验证出版日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCBRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.cbrqTextBox.Text = this.cbrqTextBox.Text.Trim();
        try
        {
            DateTime temp = DateTime.Parse(this.cbrqTextBox.Text);
            if (temp.Year < 1900 || temp.Year > 9999)
            {
                this.errorLabel.Text = "错误：出版日期必须在1900~9999之间";
                flag = false;
            }
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：出版日期不正确";
            flag = false;
        }
        return flag;
    }

    protected void ztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.ztcTextBox.Text = GlobalMethod.Semicolon(this.ztcTextBox.Text);
    }
    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text);
    }
    protected void zzdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzdwTextBox.Text = GlobalMethod.Semicolon(this.zzdwTextBox.Text);
    }
    protected void zbdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zbdwTextBox.Text = GlobalMethod.Semicolon(this.zbdwTextBox.Text);
    }
    protected void zflTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zflTextBox.Text = GlobalMethod.Semicolon(this.zflTextBox.Text);
    }

    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        String pre = this.GetDirectionPM(-1);
        if (null != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalNYBG.EDITPM_PAGE + "?wjm=" + pre;
        }

        //下一篇
        String next = this.GetDirectionPM(1);
        if (null != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalNYBG.EDITPM_PAGE + "?wjm=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        String wjm = this.GetDirectionPM(1);
        if (null == wjm)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalNYBG.EDITPM_PAGE + "?wjm=" + wjm);
        }
    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private String GetDirectionPM(int direction)
    {
        String result = null;
        //获取文件名的后四位流水号，并在其基础上加1
        int endIndex = this.wjmHyperLink.Text.LastIndexOf(".");

        String phString = this.wjmHyperLink.Text.Substring(endIndex - 4,4);

        int ph = 0;

        try
        {
            ph = Int32.Parse(phString);
        }
        catch (Exception)
        {
        }

        //当前篇号+1或-1
        if (direction > 0)
        {
            ph++;
        }
        else
        {
            ph--;
        }

        phString = ph.ToString();
        //长度为1
        if (phString.Length == 1)
        {
            phString = "000" + phString;
        }
        else if (phString.Length == 2)
        {
            phString = "00" + phString;
        }
        else if (phString.Length == 3)
        {
            phString = "0" + phString;
        }
        result = this.sybmTextBox.Text + "-" + phString + ".PDF";
        NYBG_PM nybgPM = this.nybgDAO.GetNYBG_PMByWjm(result);
        if (null == nybgPM)
        {
            return null;
        }
        return result;
    }

}